package gui;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JFileChooser;

/**
 * @author Ramy Refky 
 * */
public class LoadDataHandler {
	
	MainGui mg;
	double loadedZoomFactor;
	ArrayList<ComponentGraphics> loadedComponents;
	ArrayList<WireData> loadedWires;
	WorkArea dummy;
	
	/**
	 * Constructor initializes the handler for the load function. Where a dialog pops up 
	 * to select a file to load. And information is saved in the arrayLists for
	 * actual loading later.
	 * @param MainGui
	 * */
	public LoadDataHandler(MainGui mg){
		this.mg=mg;
		loadedComponents=new ArrayList<ComponentGraphics>();
		loadedWires=new ArrayList<WireData>();
		
		while(mg.getActions().getZoomFactor()>1){
			mg.getActions().setZoomFactor(mg.getActions().getZoomFactor()-0.25);
				
			int newWidthAndHeight= (int) (1500*mg.getActions().getZoomFactor());
				
			mg.getWorkArea().setBounds(0,0,newWidthAndHeight,newWidthAndHeight);
			mg.getVScrollBar().refreshBar();
			mg.getHScrollBar().refreshBar();
			for(int i =0;i<mg.getWorkArea().addedComponents.size();i++){
				mg.getWorkArea().addedComponents.get(i).zoomAdjust(mg.getActions().getZoomFactor());
			}
			mg.getWorkArea().adjustSelectionBox();
			for(int i=0;i<mg.getWorkArea().wiringGrid.getWires().size();i++){
				mg.getWorkArea().wiringGrid.getWires().get(i).maintainWire();
			}
			mg.getWorkArea().drawIntersections();
			mg.getWorkArea().drawWireGroupDisplays();
			mg.repaint();
		  }
		  while(mg.getActions().getZoomFactor()<1){
			mg.getActions().setZoomFactor(mg.getActions().getZoomFactor()+0.25);
			 
			int newWidthAndHeight= (int) (1500*mg.getActions().getZoomFactor());
				
			mg.getWorkArea().setBounds(0,0,newWidthAndHeight,newWidthAndHeight);
			mg.getVScrollBar().refreshBar();
			mg.getHScrollBar().refreshBar();
			for(int i =0;i<mg.getWorkArea().addedComponents.size();i++){
				mg.getWorkArea().addedComponents.get(i).zoomAdjust(mg.getActions().getZoomFactor());
			}
			for(int i=0;i<mg.getWorkArea().wiringGrid.getWires().size();i++){
				mg.getWorkArea().wiringGrid.getWires().get(i).maintainWire();
			}
			mg.getWorkArea().drawIntersections();
			mg.getWorkArea().drawWireGroupDisplays();
			mg.repaint();
		  }
		  
		try {
			File loadFile=null;
			JFileChooser jf = new JFileChooser();
			jf.setDialogTitle("Load File...");
			jf.addChoosableFileFilter(new SBFileFilter());
			
			jf.setVisible(true);
			if (jf.showDialog(null,"Load") == JFileChooser.APPROVE_OPTION){
				loadFile = jf.getSelectedFile();
				
				String path = loadFile.getAbsolutePath();
				if(!path.endsWith(".sb")){
					loadFile = new File(path + ".sb");
				}
			}
			Scanner reader = new Scanner(new FileInputStream(loadFile));
			
			while (reader.hasNext()) { 
			    String s = reader.next();
			    if(s.equals("<ZoomFactor>")){
			    	loadedZoomFactor=reader.nextDouble();
			    }
			    if(s.equals("<AndComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new AndComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NandComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NandComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<OrComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new OrComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<XorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new XorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<XnorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new XnorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NotComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NotComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<BufferComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new BufferComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<TSBufferComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TSBufferComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NodeComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NodeComponentGraphics(mg,x,y,width,height,numOfInputs,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<DFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new DFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<TFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<JKFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new JKFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<SRFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new SRFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<CustomComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	String text= reader.next();
			    	reader.next();reader.next();
			    	int clockPin= reader.nextInt();
			    	reader.next();reader.next();
			    	String details= reader.next();
			    	reader.next();reader.next();
			    	boolean showDetails=false;
			    	if(details.equals("true")){
			    		showDetails=true;
			    	}
			    	loadedComponents.add(new CustomComponentGraphics(mg,x,y,width,height,numOfInputs,
			    			numOfOutputs,text,clockPin,showDetails));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<TextComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	String text= reader.next();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TextComponentGraphics(mg,x,y,width,height,text));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<ModuleComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	File file= new File(reader.next());
			    	reader.next();reader.next();
			    	String details= reader.next();
			    	reader.next();reader.next();
			    	boolean showDetails=false;
			    	if(details.equals("true")){
			    		showDetails=true;
			    	}
			    	loadedComponents.add(new ModuleComponentGraphics(mg,x,y,width,height,file,showDetails));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<SplitterComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	int option= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new SplitterComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs,option));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<GrouperComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	int option= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new GrouperComponentGraphics(mg,x,y,width,height,numOfOutputs,numOfInputs,option));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<wire>")){
			    	ComponentPin firstPin = null;
			    	ComponentPin secondPin = null;
			    	reader.next();
			    	int id1=reader.nextInt();
			    	reader.next();reader.next();
			    	int id2=reader.nextInt();
			    	
			    	for(int i=0;i<loadedComponents.size();i++){
			    		for(int j=0;j<loadedComponents.get(i).getInputPins().size();j++){
			    			if(loadedComponents.get(i).getInputPins().get(j).getID()==id1){
			    				firstPin=loadedComponents.get(i).getInputPins().get(j);
			    			}
			    			if(loadedComponents.get(i).getInputPins().get(j).getID()==id2){
			    				secondPin=loadedComponents.get(i).getInputPins().get(j);
			    			}
			    		}
			    		for(int j=0;j<loadedComponents.get(i).getOutputPins().size();j++){
			    			if(loadedComponents.get(i).getOutputPins().get(j).getID()==id1){
			    				firstPin=loadedComponents.get(i).getOutputPins().get(j);
			    			}
			    			if(loadedComponents.get(i).getOutputPins().get(j).getID()==id2){
			    				secondPin=loadedComponents.get(i).getOutputPins().get(j);
			    			}
			    		}
			    	}
			    	reader.next();reader.next();
			    	int numOfSegments = reader.nextInt();
			    	ArrayList<Double> offsets = new ArrayList<Double>();
			    	reader.next();
			    	
			    	for(int i=0;i<numOfSegments-2;i++){
			    		reader.next();reader.next();
			    		double offset = reader.nextDouble();
			    		offsets.add(offset);
			    	}
			    	if(numOfSegments==3){
			    		loadedWires.add(new WireData(firstPin,secondPin,offsets.get(0),0,0,0));
			    	}else{
			    		if(numOfSegments==5){
				    		loadedWires.add(new WireData(firstPin,secondPin,0,offsets.get(0),offsets.get(1),offsets.get(2)));
				    	}
			    	}
			    }
			}
		}
		catch (Exception e) {}
		
	}
	
	/**
	 * Constructor initializes the handler for the load module component. Where the file to load 
	 * is passed as a parameter. Information is saved in the arrayLists, and a dummy workArea
	 * is used so that the image of the circuit can be retrieved later.
	 * @param MainGui, LoadFile
	 * */
	public LoadDataHandler(MainGui mg,File loadFile){
		
		this.mg=mg;
		dummy=new WorkArea(mg);
		loadedComponents=new ArrayList<ComponentGraphics>();
		loadedWires=new ArrayList<WireData>();
		
		dummy.getWiringGrid().getWires().clear();
		dummy.addedComponents.clear();
		dummy.removeAll();
		dummy.repaint();
		double zoomFactor = mg.getActions().getZoomFactor();
		while(zoomFactor>1){
			zoomFactor=(zoomFactor-0.25);
				
			int newWidthAndHeight= (int) (1500*zoomFactor);
				
			dummy.setBounds(0,0,newWidthAndHeight,newWidthAndHeight);
			mg.getVScrollBar().refreshBar();
			mg.getHScrollBar().refreshBar();
			for(int i =0;i<dummy.addedComponents.size();i++){
				dummy.addedComponents.get(i).zoomAdjust(zoomFactor);
			}
			dummy.adjustSelectionBox();
			for(int i=0;i<dummy.wiringGrid.getWires().size();i++){
				dummy.wiringGrid.getWires().get(i).maintainWire();
			}
			dummy.drawIntersections();
			dummy.drawWireGroupDisplays();
			mg.repaint();
		  }
		  while(zoomFactor<1){
			  zoomFactor=(zoomFactor+0.25);
			 
			int newWidthAndHeight= (int) (1500*zoomFactor);
				
			dummy.setBounds(0,0,newWidthAndHeight,newWidthAndHeight);
			mg.getVScrollBar().refreshBar();
			mg.getHScrollBar().refreshBar();
			for(int i =0;i<dummy.addedComponents.size();i++){
				dummy.addedComponents.get(i).zoomAdjust(zoomFactor);
			}
			for(int i=0;i<dummy.wiringGrid.getWires().size();i++){
				dummy.wiringGrid.getWires().get(i).maintainWire();
			}
			dummy.drawIntersections();
			dummy.drawWireGroupDisplays();
			mg.repaint();
		  }
		
		try {
			
			Scanner reader = new Scanner(new FileInputStream(loadFile));
			
			while (reader.hasNext()) { 
			    String s = reader.next();
			    if(s.equals("<ZoomFactor>")){
			    	loadedZoomFactor=reader.nextDouble();
			    }
			    if(s.equals("<AndComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new AndComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NandComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NandComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<OrComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new OrComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<XorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new XorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<XnorComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new XnorComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NotComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NotComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<BufferComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new BufferComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<TSBufferComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TSBufferComponentGraphics(mg,x,y,width,height,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<NodeComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new NodeComponentGraphics(mg,x,y,width,height,numOfInputs,numOfInputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	reader.next();reader.next();
		    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setID(reader.nextInt());
		    		reader.next();reader.next();
			    	loadedComponents.get(loadedComponents.size()-1).getOutputPin().setConnections(reader.nextInt());
			    	reader.next();reader.next();
			    	boolean grouped = reader.nextBoolean();
			    	if(grouped){
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPin().setAsGrouped();
			    	}
			    	reader.next();reader.next();
			    }
			    if(s.equals("<DFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new DFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<TFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<JKFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new JKFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<SRFFComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new SRFFComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<CustomComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	String text= reader.next();
			    	reader.next();reader.next();
			    	int clockPin= reader.nextInt();
			    	reader.next();reader.next();
			    	String details= reader.next();
			    	reader.next();reader.next();
			    	boolean showDetails=false;
			    	if(details.equals("true")){
			    		showDetails=true;
			    	}
			    	loadedComponents.add(new CustomComponentGraphics(mg,x,y,width,height,numOfInputs,
			    			numOfOutputs,text,clockPin,showDetails));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<TextComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	String text= reader.next();
			    	reader.next();reader.next();
			    	loadedComponents.add(new TextComponentGraphics(mg,x,y,width,height,text));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<ModuleComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	File file= new File(reader.next());
			    	reader.next();reader.next();
			    	String details= reader.next();
			    	reader.next();reader.next();
			    	boolean showDetails=false;
			    	if(details.equals("true")){
			    		showDetails=true;
			    	}
			    	loadedComponents.add(new ModuleComponentGraphics(mg,x,y,width,height,file,showDetails));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<SplitterComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	int option= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new SplitterComponentGraphics(mg,x,y,width,height,numOfInputs,numOfOutputs,option));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<GrouperComponentGraphics>")){
			    	reader.next();
			    	int x = reader.nextInt();
			    	reader.next();reader.next();
			    	int y = reader.nextInt();
			    	reader.next();reader.next();
			    	int width = reader.nextInt();
			    	reader.next();reader.next();
			    	int height = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfInputs = reader.nextInt();
			    	reader.next();reader.next();
			    	int numOfOutputs= reader.nextInt();
			    	reader.next();reader.next();
			    	int option= reader.nextInt();
			    	reader.next();reader.next();
			    	loadedComponents.add(new GrouperComponentGraphics(mg,x,y,width,height,numOfOutputs,numOfInputs,option));
			    	for(int i=0;i<numOfInputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getInputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    	reader.next();reader.next();
			    	
			    	for(int i=0;i<numOfOutputs;i++){
			    		reader.next();reader.next();
			    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setID(reader.nextInt());
			    		reader.next();reader.next();
				    	loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setConnections(reader.nextInt());
				    	reader.next();reader.next();
				    	boolean grouped = reader.nextBoolean();
				    	if(grouped){
				    		loadedComponents.get(loadedComponents.size()-1).getOutputPins().get(i).setAsGrouped();
				    	}
				    	reader.next();reader.next();
				    }
			    }
			    if(s.equals("<wire>")){
			    	ComponentPin firstPin = null;
			    	ComponentPin secondPin = null;
			    	reader.next();
			    	int id1=reader.nextInt();
			    	reader.next();reader.next();
			    	int id2=reader.nextInt();
			    	
			    	for(int i=0;i<loadedComponents.size();i++){
			    		for(int j=0;j<loadedComponents.get(i).getInputPins().size();j++){
			    			if(loadedComponents.get(i).getInputPins().get(j).getID()==id1){
			    				firstPin=loadedComponents.get(i).getInputPins().get(j);
			    			}
			    			if(loadedComponents.get(i).getInputPins().get(j).getID()==id2){
			    				secondPin=loadedComponents.get(i).getInputPins().get(j);
			    			}
			    		}
			    		for(int j=0;j<loadedComponents.get(i).getOutputPins().size();j++){
			    			if(loadedComponents.get(i).getOutputPins().get(j).getID()==id1){
			    				firstPin=loadedComponents.get(i).getOutputPins().get(j);
			    			}
			    			if(loadedComponents.get(i).getOutputPins().get(j).getID()==id2){
			    				secondPin=loadedComponents.get(i).getOutputPins().get(j);
			    			}
			    		}
			    	}
			    	reader.next();reader.next();
			    	int numOfSegments = reader.nextInt();
			    	ArrayList<Double> offsets = new ArrayList<Double>();
			    	reader.next();
			    	
			    	for(int i=0;i<numOfSegments-2;i++){
			    		reader.next();reader.next();
			    		double offset = reader.nextDouble();
			    		offsets.add(offset);
			    	}
			    	if(numOfSegments==3){
			    		loadedWires.add(new WireData(firstPin,secondPin,offsets.get(0),0,0,0));
			    	}else{
			    		if(numOfSegments==5){
				    		loadedWires.add(new WireData(firstPin,secondPin,0,offsets.get(0),offsets.get(1),offsets.get(2)));
				    	}
			    	}
			    }
			}
		}
		catch (Exception e) {}
		
	}
	/**
	 * Loads the information from the arrayLists onto the workArea.
	 * */
	public void load(){
		if(loadedComponents.size()>0){
			mg.getWorkArea().getWiringGrid().getWires().clear();
			mg.getWorkArea().getAddedComponents().clear();
			mg.getWorkArea().removeAll();
			mg.getWorkArea().repaint();
		}
		//addComponents
		for(int i=0;i<loadedComponents.size();i++){
			mg.getWorkArea().getAddedComponents().add(loadedComponents.get(i));
			mg.getWorkArea().add(mg.getWorkArea().getAddedComponents().get(mg.getWorkArea().
					getAddedComponents().size()-1));
			mg.getWorkArea().setComponentZOrder(mg.getWorkArea().getAddedComponents().
					get(mg.getWorkArea().getAddedComponents().size()-1),0);
			mg.getWorkArea().repaint();
		}
		//add wires
		for(int i=0;i<loadedWires.size();i++){
			Wire w = new Wire(mg,loadedWires.get(i).getFirstPin(),
					loadedWires.get(i).getSecondPin(),
					loadedWires.get(i).getOffsetTypeA(),
					loadedWires.get(i).getOffsetTypeB(),
					loadedWires.get(i).getOffsetTypeC(),
					loadedWires.get(i).getOffsetTypeD());
			if(loadedWires.get(i).getFirstPin().isGrouped()||loadedWires.get(i).getSecondPin().isGrouped()){
				w.setAsGrouped();
			}
			mg.getWorkArea().getWiringGrid().addWire(w);
		}
		mg.getWorkArea().drawIntersections();
		mg.getWorkArea().drawWireGroupDisplays();
		mg.getWorkArea().repaint();
	}
	/**
	 * Gets the image of the circuit produced from the dummy workArea.
	 * @return BufferedImage
	 * */
	public BufferedImage loadModule(){
	
	BufferedImage bi = new BufferedImage(dummy.getWidth()+100,dummy.getHeight()+100, BufferedImage.TYPE_INT_ARGB); 
	dummy.getAddedComponents().clear();
	dummy.getWiringGrid().getWires().clear();
	dummy.removeAllWireIntersections();
		//addComponents
		for(int i=0;i<loadedComponents.size();i++){
			dummy.getAddedComponents().add(loadedComponents.get(i));
			dummy.add(dummy.getAddedComponents().get(dummy.
					getAddedComponents().size()-1));
			dummy.setComponentZOrder(dummy.getAddedComponents().
					get(dummy.getAddedComponents().size()-1),0);
			dummy.repaint();
		}
		//add wires
		for(int i=0;i<loadedWires.size();i++){
			Wire w = new Wire(dummy,loadedWires.get(i).getFirstPin(),
					loadedWires.get(i).getSecondPin(),
					loadedWires.get(i).getOffsetTypeA(),
					loadedWires.get(i).getOffsetTypeB(),
					loadedWires.get(i).getOffsetTypeC(),
					loadedWires.get(i).getOffsetTypeD());
			if(loadedWires.get(i).getFirstPin().isGrouped()||loadedWires.get(i).getSecondPin().isGrouped()){
				w.setAsGrouped();
			}
			dummy.getWiringGrid().addWire(w);
		}
		dummy.drawIntersections();
		dummy.drawWireGroupDisplays();
		dummy.repaint();
		
		dummy.hideSelectionBox();
		dummy.deselectAllComponents();
		
		if(dummy.addedComponents.size()>0){
			int minX=dummy.getWidth();
			int minY=dummy.getHeight();
			int maxX=0;
			int maxY=0;
			for(int i=0;i<dummy.addedComponents.size();i++){
				if(dummy.addedComponents.get(i).getX()<minX){
					minX=dummy.addedComponents.get(i).getX();
				}
				if(dummy.addedComponents.get(i).getY()<minY){
					minY=dummy.addedComponents.get(i).getY();
				}
				if((dummy.addedComponents.get(i).getX()+dummy.addedComponents.get(i).getWidth())>maxX){
					maxX=dummy.addedComponents.get(i).getX()+dummy.addedComponents.get(i).getWidth();
				}
				if((dummy.addedComponents.get(i).getY()+dummy.addedComponents.get(i).getHeight())>maxY){
					maxY=dummy.addedComponents.get(i).getY()+dummy.addedComponents.get(i).getHeight();
				}
			}
			for(int i=0;i<dummy.getWiringGrid().getWires().size();i++){
				Wire w = dummy.getWiringGrid().getWires().get(i);
				for(int j=0;j<w.getWireSegments().size();j++){
					WireSegment ws=w.getWireSegments().get(j);
					
					if(ws.getX()<minX){
						minX=ws.getX();
					}
					if(ws.getY()<minY){
						minY=ws.getY();
					}
					if((ws.getX()+ws.getWidth())>maxX){
						maxX=ws.getX()+ws.getWidth();
					}
					if((ws.getY()+ws.getHeight())>maxY){
						maxY=ws.getY()+ws.getHeight();
					}
				}
			}
			
			Graphics2D g5 = bi.createGraphics();
			dummy.showGrid=false;
			dummy.setOpaque(false);
			dummy.paint(g5);
			dummy.showGrid=true;
			dummy.setOpaque(true);
			
			bi=bi.getSubimage(minX, minY, maxX-minX, maxY-minY);
			g5.dispose();
		}
		return bi;
	}
	/**
	 * Gets the total number of unconnected input ComponentPins from the loaded arayList.
	 * @return int
	 * */
	public int getLoadedModuleInputs(){
		int count=0;
		
		for(int i=0;i<loadedComponents.size();i++){
			ComponentGraphics cg = loadedComponents.get(i);
			for(int j=0;j<cg.getInputPins().size();j++){
				if(cg.getInputPins().get(j).getConnections()==0){
					count++;
				}
			}
		}
		return count;
	}
	/**
	 * Gets the total number of unconnected output ComponentPins from the loaded arayList.
	 * @return int
	 * */
	public int getLoadedModuleOutputs(){
		int count=0;
		
		for(int i=0;i<loadedComponents.size();i++){
			ComponentGraphics cg = loadedComponents.get(i);
			for(int j=0;j<cg.getOutputPins().size();j++){
				if(cg.getOutputPins().get(j).getConnections()==0){
					count++;
				}
			}
		}
		return count;
	}
}